package reflect;

import reflect.annotations.AutoRunClass;
import reflect.annotations.AutoRunMethod;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.util.Arrays;

public class Test4 {
    public static void main(String[] args) throws Exception {
        File dir = new File(Test4.class.getResource(".").toURI());
        File[] subs = dir.listFiles(f -> f.getName().endsWith(".class"));
        for(File sub : subs){
            String className = sub.getName().replace(".class","");
            Class cls =Class.forName(Test4.class.getPackage().getName()+"."+className);
//            System.out.println(cls.getName());

            if(cls.isAnnotationPresent(AutoRunClass.class)){
                Method[] mds = cls.getDeclaredMethods();
                Object o = cls.newInstance();
//                System.out.println(Arrays.toString(mds));
                for (Method md : mds){
                    if (md.isAnnotationPresent(AutoRunMethod.class)){
                        md.invoke(o);
                    }
                }
            }
        }
    }
}
